require 'rubygems'
require 'rufus/scheduler'

module Installr::Client::Command
  class Schedule < Base
    def create(schedule_name)
      puts "[schedule:create] create schedule #{schedule_name}" if @options[:verbose]
      @log.info("[schedule:create] create schedule #{schedule_name}")

      if @options[:cron_format]
        if @storage.create_schedule(schedule_name, @options[:cron_format]) != 0
          puts "[schedule:create] cannot create schedule #{schedule_name}"
          @log.error("[schedule:create] cannot create schedule #{schedule_name}")
          return -1
	else
          return 0
        end
      else
        puts "[schedule:create] missing the --schedule option"
        @log.error("[schedule:create] missing the --schedule option")
        return -1
      end
    end

    def delete(schedule_name)
      puts "[schedule:delete] delete schedule #{schedule_name}" if @options[:verbose]
      @log.info("[schedule:delete] delete schedule #{schedule_name}")

      if @storage.delete_schedule(schedule_name) != 0
        puts "[schedule:delete] cannot delete schedule #{schedule_name}"
        @log.error("[schedule:delete] cannot delete schedule #{schedule_name}")
        return -1
      else
        return 0
      end
    end

    def info(schedule_name)
      puts "[schedule:info] info schedule #{schedule_name}" if @options[:verbose]
      puts "[schedule:info] info schedule #{schedule_name}"

      schedule = @storage.info_schedule(schedule_name)
      if schedule
        puts "information of the schedule #{schedule_name}:"
        schedule.each { |field| print "#{field} " }
        puts " ---"
        return 0
      else
        puts "[schedule:info] cannot find schedule #{schedule_name}"
        @log.error("[schedule:info] cannot find schedule #{schedule_name}")
        return -1
      end
    end

    def list
      puts "[schedule:list] list schedules" if @options[:verbose]
      @log.info("[schedule:list] list schedules")

      schedules = @storage.list_schedules
      if schedules
        puts "current schedule listgins:"
        schedules.each { |schedule| print "#{schedule} " }
	puts " ---"
      else
        puts "[schedule:list] cannot find any schedule"
      end
      return 0
    end

    def execute
      puts "[schedule:execute] execute schedules" if @options[:verbose]
      @log.info("[schedule:execute] execute schedules")

      schedules = @storage.list_schedules
      if schedules
        job_cnt = 0
        scheduler = Rufus::Scheduler.start_new
        schedules.each do
          |schedule|
          puts "schedule name: #{schedule}"
          schedule_info = @storage.info_schedule(schedule)
          puts "activated? " + schedule_info[0]
	  next if schedule_info[0] == 'D'
          puts "cron_format: #{schedule_info[1]}"
          scenarios = @storage.list_scenarios(schedule)
	  next if scenarios.length == 0
          scenarios.each do
            |scenario|
            puts "scenario name: #{scenario}"
	    job_cnt += 1
            scheduler.cron schedule_info[1] do
              IO.popen('ruby -S Installr execute scenario ' + scenario) do |pipe|
                pipe.sync = true
                while str = pipe.gets
                  puts str
                end
              end
            end
          end
        end
        scheduler.join if job_cnt > 0
        return 0
      else
        puts "[schedule:execute] cannot find any schedule"
        @log.error("[schedule:execute] cannot find any schedule")
        return -1
      end
    end

    def activate(schedule_name)
      puts "[schedule:activate] activate schedule #{schedule_name}" if @options[:verbose]
      @log.info("[schedule:activate] activate schedule #{schedule_name}")

      if @storage.activate_schedule(schedule_name) != 0
        puts "[schedule:activate] cannot activate schedule #{schedule_name}"
        @log.error("[schedule:activate] cannot activate schedule #{schedule_name}")
        return -1
      else
        return 0
      end
    end

    def deactivate(schedule_name)
      puts "[schedule:deactivate] deactivate schedule #{schedule_name}" if @options[:verbose]
      @log.info("[schedule:deactivate] deactivate schedule #{schedule_name}")

      if @storage.deactivate_schedule(schedule_name) != 0
        puts "[schedule:deactivate] cannot deactivate schedule #{schedule_name}"
        @log.error("[schedule:deactivate] cannot deactivate schedule #{schedule_name}")
        return -1
      else
        return 0
      end
    end
  end
end
